/*
 * Candidato.cpp
 *
 *  Created on: Oct 15, 2011
 *      Author: lore
 */

#include "Candidato.h"

Candidato::Candidato() {
}

Candidato::~Candidato() {
}

unsigned long Candidato::getUnifiedId(unsigned long idL, unsigned long idC, unsigned long idV) {
	unsigned long unifiedId = 0;

	stringstream ss;

	ss.clear();
	char convertedDec[128];
	sprintf(convertedDec,"%lu%lu%lu",idL,idC,idV);
	ss << convertedDec;

	unifiedId = strtoul((ss.str()).c_str(),NULL,0);

	return unifiedId;
}

unsigned long Candidato::getId() {
	return getUnifiedId(idLista,idCargo,idVotante);
}

void Candidato::setIdLista(unsigned long idlist){
	idLista = idlist;
}
unsigned long Candidato::getIdLista(){
	return idLista;
}

void Candidato::setIdVotante(unsigned long idvot){
	idVotante=idvot;
}

unsigned long Candidato::getIdVotante(){
	return idVotante;
}

void Candidato::setIdCargo(unsigned long idcar){
	idCargo=idcar;
}

unsigned long Candidato:: getCargo(){
	return idCargo;
}

char* Candidato::pack() {
	char* buffer = new char[packLen()];

	char*ptr = reinterpret_cast<char*>(&idVotante);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i] = *ptr;
		ptr++;
	}
	
	ptr = reinterpret_cast<char*>(&idLista);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i+sizeof(unsigned long)] = *ptr;
		ptr++;
	}
	
	ptr = reinterpret_cast<char*>(&idCargo);
		for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
			buffer[i+2*sizeof(unsigned long)] = *ptr;
			ptr++;
		}
	
	return buffer;
}

bool Candidato::unpack(char* buffer, unsigned long bufferLen) {

	char*ptr = reinterpret_cast<char*>(&idVotante);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i];
		ptr++;
	}

	ptr = reinterpret_cast<char*>(&idLista);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i+sizeof(unsigned long)];
		ptr++;
	}

	ptr = reinterpret_cast<char*>(&idCargo);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i+2*sizeof(unsigned long)];
		ptr++;
	}
	return true;
}

unsigned long Candidato::packLen() {
	unsigned long tam= 3*sizeof(unsigned long);
	return (tam);
}
